package org.jaaksi.kottlindemo.test

import android.graphics.Typeface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_test_rule_view.btnChange
import kotlinx.android.synthetic.main.activity_test_rule_view.rulerView
import kotlinx.android.synthetic.main.activity_test_rule_view.textView
import libcore.view.RulerView
import org.jaaksi.kottlindemo.R

class TestRuleViewActivity : AppCompatActivity() {
    // 默认为cm
    private var isCm = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_test_rule_vertical)
        setContentView(R.layout.activity_test_rule_view)
        val typeface = Typeface.create(Typeface.MONOSPACE, Typeface.BOLD_ITALIC)
        textView.typeface = typeface
        rulerView.setTextTypeFace(typeface)
        textView.text = rulerView.selectedValue.toString()
        rulerView.setOnValueChangeListener(object : RulerView.OnValueChangedListener {
            override fun onValueChanged(value: Float) {
                textView.text = if (isCm) {
                    "${value.toInt()} cm"
                } else {
                    "${value.toInt() / 12}`${value.toInt() % 12}`` ft"
                }
            }
        })
        rulerView.setScaleValueFormatter(object : RulerView.ScaleValueFormatter {
            override fun format(value: Float): String {
                if (isCm) return value.toInt().toString()

                return "${value.toInt() / 12}`"
            }
        })

        // 小数有问题
        rulerView.setValue(
            120f,
            103f,
            250f,
            1f, 10,
            minOptionalValue = 110f,
            maxOptionalValue = 200f
        )


//        rulerView2.setValue(110f, 108f, 256f, 1f, 10)
        btnChange.setOnClickListener {
            isCm = !isCm
            if (!isCm) {
                rulerView.setValue(
                    5 * 12f + 7,
                    3 * 12f + 3,
                    8 * 12f + 2,
                    1f,
                    12
                )
            } else {
                rulerView.setValue(
                    170f,
                    100f,
                    250f,
                    1f, 10
                )
            }
        }

        var num = 10
        when {
            num > 0 -> {
                println()
            }
            num < 20 -> {
                println()
            }
        }


    }

    private fun addRuleView() {
        // 代码中创建RulerView
//        container.addView(RulerView(this), -2, -1)
    }

    // 四舍五入保留1位小数
    fun caculate(value: Float): Float {
        return String.format("%.1f", value).toFloat()
    }
}